package com.dingreading.cloud.soms.service.impl;

import com.dingreading.cloud.common.util.NepUtil;
import com.dingreading.cloud.common.util.PageUtil;
import com.dingreading.cloud.soms.dto.ReceiptRegisterDto;
import com.dingreading.cloud.soms.entity.ReceiptRegister;
import com.dingreading.cloud.soms.entity.table.ReceiptRegisterTableDef;
import com.dingreading.cloud.soms.mapper.ReciptRegisterMapper;
import com.dingreading.cloud.soms.service.ReceiptRegisterService;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.update.UpdateChain;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 服务层实现。
 *
 * @author Administrator
 * @since 2025-05-26
 */
@Service
public class ReceiptRegisterServiceImpl extends BaseServiceImpl<ReciptRegisterMapper, ReceiptRegister, ReceiptRegisterTableDef> implements ReceiptRegisterService {

    @Override
    protected ReceiptRegisterTableDef getTable() {
        return ReceiptRegisterTableDef.receiptRegister;
    }

    @Override
    public Page<ReceiptRegisterDto> pageList(
            PageUtil pageUtil, String storeUid, String keyword, Integer enabled,
            String beginDate, String endDate
    ) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.storeUid.eq(storeUid));
        if (StringUtils.isNotBlank(keyword))
            condition.and(table.userName.like(keyword)
                    .or(table.amount.eq(keyword))
                    .or(table.saleName.eq(keyword))
                    .or(table.payment.eq(keyword))
            );
        if (StringUtils.isNotBlank(beginDate) && StringUtils.isNotBlank(endDate))
            condition.and(table.payDate.between(beginDate, endDate));

        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition)
                .orderBy(table.id.desc());

        return getMapper().paginateWithRelationsAs(Page.of(pageUtil.getPage(), pageUtil.getRows()), wrapper, ReceiptRegisterDto.class);
    }

    @Override
    public ReceiptRegister getByUid(String storeUid, String uid) {
        return getMapper().selectOneByCondition(table.uid.eq(uid).and(table.storeUid.eq(storeUid)));
    }

    @Override
    public ReceiptRegisterDto dtoGetByUid(String storeUid, String uid) {
        QueryWrapper wrapper = QueryWrapper.create()
                .where(table.uid.eq(uid).and(table.storeUid.eq(storeUid)));
        return getMapper().selectOneWithRelationsByQueryAs(wrapper, ReceiptRegisterDto.class);
    }

    @Override
    public int deleteByUid(String uid) {
        return getMapper().deleteByCondition(table.uid.eq(uid));
    }

    @Override
    public List<ReceiptRegisterDto> unbindList(String storeUid, String receiptUid) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.storeUid.eq(storeUid))
                .and(table.status.eq(0))
                .and(table.type.eq(1));
        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition);
        return getMapper().selectListWithRelationsByQueryAs(wrapper, ReceiptRegisterDto.class);
    }

    @Override
    public boolean unbindReceipt(String storeUid, String orderCode, String receiptUid, String staffUid, String staffName) {
        QueryCondition condition = table.uid.eq(receiptUid)
                .and(table.orderCode.eq(orderCode))
                .and(table.storeUid.eq(storeUid));
        return UpdateChain.of(ReceiptRegister.class)
                .set(table.orderCode, "")
                .set(table.status, 0)
                .set(table.lastStaffUid, NepUtil.nullToZero(staffUid))
                .set(table.lastStaffName, NepUtil.nullToZero(staffName))
                .where(condition)
                .update();
    }

    @Override
    public boolean updateStatus(String storeUid, String orderCode, String receiptUid, String staffUid, String staffName) {
        return UpdateChain.of(ReceiptRegister.class)
                .set(table.storeUid, storeUid)
                .set(table.orderCode, orderCode)
                .set(table.status, 1)
                .set(table.lastStaffUid, NepUtil.nullToZero(staffUid))
                .set(table.lastStaffName, NepUtil.nullToZero(staffName))
                .where(table.uid.eq(receiptUid))
                .update();
    }
}
